/*
 * @Description: 接口响应拦截器-正常来说只有结果正常才会走
 * @Author: yeke
 * @Date: 2023-10-25 20:32:12
 * @LastEditors: yeke
 * @LastEditTime: 2024-03-29 11:06:46
 */
import {
  Injectable,
  NestInterceptor,
  CallHandler,
  HttpStatus,
} from '@nestjs/common';
import { map } from 'rxjs/operators';
import { Observable } from 'rxjs';
import { ResponseData } from './response';

@Injectable()
export class ResponsInterceptoreBase<T> implements NestInterceptor {
  intercept(context, next: CallHandler): Observable<ResponseData<T>> {
    return next.handle().pipe(
      map((data) => {
        return {
          data: data.data ? data.data : null,
          code: data.code ? data.code : HttpStatus.OK,
          message: data.message ? data.message : '成功',
        };
      }),
    );
  }
}
